/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.ipccenter.toster.ejb;

import com.sun.xml.ws.api.tx.at.Transactional;
import edu.ipccenter.toster.Name;
import edu.ipccenter.toster.TestCase;
import edu.ipccenter.toster.User;
import edu.ipccenter.toster.UserUtils;
import edu.ipccenter.toster.jpa.JPATestCase;
import edu.ipccenter.toster.jpa.JPAUser;
import java.math.BigInteger;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author 7
 */

@Stateless
@Transactional
@TransactionAttribute(TransactionAttributeType.REQUIRED )
public class UserUtilsBean implements UserUtils {

    @PersistenceContext(unitName="TosterPU")
    private EntityManager em;
    
    @Override
    public User findByID(BigInteger id) {
          User user = null;  
       
        JPAUser us;
        Query q = em.createQuery("select c from JPAUser c where c.userId =:myid");
        q.setParameter("myid", id);
        try{
        us = (JPAUser) q.getSingleResult();
        }catch(Exception ex){
            return null;
        }
        
        
        InitialContext ic;
        try {
            ic = new InitialContext();
            user = (User) ic.lookup("java:global/Tost/Tost-ejb/User");
        } catch (NamingException ex) {
            Logger.getLogger(TestCaseUtilsBean.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }
           
       
      
        user.setDatum(us);       
         
        return user;
    }

    @Override
    public Collection<Name> findByName(String keyText, int upperLimit) {
      Collection<Name> names = new HashSet<Name>();
		
                Query q = em.createQuery("select c from JPAUser c where c.userName LIKE :keyText");
                q.setParameter("keyText", '%'+keyText+'%');
                
                List<JPAUser> jtest =(List<JPAUser>) q.getResultList();
                
                for(int i = 0; i<jtest.size() && i< upperLimit; i++){
                    Name n = new Name();
                    n.setID(jtest.get(i).getUserId());
                    n.setName(jtest.get(i).getUserName());
                    names.add(n);
                }
                               
                
		return names;
    }

    @Override
    public Collection<Name> findBySurName(String keyText, int upperLimit) {
         Collection<Name> names = new HashSet<Name>();
		
                Query q = em.createQuery("select c from JPAUser c where c.userSurname LIKE :keyText");
                q.setParameter("keyText", '%'+keyText+'%');
                
                List<JPAUser> jtest =(List<JPAUser>) q.getResultList();
                
                for(int i = 0; i<jtest.size() && i< upperLimit; i++){
                    Name n = new Name();
                    n.setID(jtest.get(i).getUserId());
                    n.setName(jtest.get(i).getUserSurname());
                    names.add(n);
                }
                               
                
		return names;
    }

    @Override
    public User create(String name, String surname) {
       

            User user = null;
            InitialContext ic;
            try {
                ic = new InitialContext();
                user = (User) ic.lookup("java:global/Tost/Tost-ejb/User");
            } catch (NamingException ex) {
                Logger.getLogger(TestCaseUtilsBean.class.getName()).log(Level.SEVERE, null, ex);
                return null;
            }

            JPAUser ju = new JPAUser();
            ju.setUserName(name);
            ju.setUserSurname(surname);
            
            em.merge(ju);
            user.setDatum(ju);


            return user;
    }
    
}
